﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Net.Mail;

namespace HUBS1.HCMS.BO
{
    public class Function
    {
        #region 是否是日期
        public static bool IsDate(string indate)
        {
            bool isdate = true;
            try
            {
                DateTime.Parse(indate);
            }
            catch
            {
                isdate = false;
            }
            return isdate;
        }
        #endregion

        #region 是否是整数
        public static bool IsInt(string inint)
        {
            bool isint = true;
            try
            {
                int.Parse(inint);
            }
            catch
            {
                isint = false;
            }
            return isint;
        }      
        #endregion

        #region 是否为正整数
        public static bool IsSignlessInt(string inint)
        {            
            return IsInt(inint) && int.Parse(inint) > 0;           
        }
        #endregion

        #region 随机字符
        public static string rndnum()
        {
            string vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p" +
            ",q,r,s,t,u,v,w,x,y,z";
            string[] vcarray = vchar.Split(',');
            string vnum = "";
            int temp = -1;
            System.Random rand = new Random();
            for (int i = 1; i < 9; i++)
            {
                if (temp != -1)
                {
                    rand = new Random(i * temp * unchecked((int)DateTime.Now.Ticks));
                }
                int t = rand.Next(35);
                if (temp != -1 && temp == t)
                {
                    return rndnum();
                }
                temp = t;
                vnum += vcarray[t];
            }
            vnum += DateTime.Now.Ticks.ToString();
            return vnum;
        }
        #endregion

        #region 验证电子邮件地址
        /// <summary>
        /// 验证电子邮件地址
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsEmail(string s)
        {
            Match match = Regex.Match(s, @"^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$");

            return match.Success;

        }
        #endregion

        #region 国家代码
        /// <summary>
        /// 国家代码
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsCountryCode(string s)
        {
            Match match = Regex.Match(s, @"^\+?\d+");

            return match.Success;
        }
        #endregion

        #region 省编号
        /// <summary>
        /// 省编号
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsAreaCode(string s)
        {
            Match match = Regex.Match(s, @"^\(\d+\)$|^\d+$");

            return match.Success;

        }
        #endregion

        #region 自定义正则验证
        /// <summary>
        /// 自定义正则验证
        /// </summary>
        /// <param name="s"></param>
        /// <param name="regx"></param>
        /// <returns></returns>
        public static bool CustomRegx(string s, string regx)
        {
            Match match = Regex.Match(s, regx);
            return match.Success;
        }
        #endregion

        #region 获取错误信息
        public static string GetErrorText(string errorcode,int texttype)
        {
            string errortext = "";
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(System.Configuration.ConfigurationManager.AppSettings["Errorcodefile"]);
                XmlNodeList codelist = doc.SelectNodes("//OTA_CodeTable/Codes/Code");
                foreach (XmlNode errornode in codelist)
                {
                    if (errornode.Attributes[0].Value == errorcode)
                    {
                        XmlNode errorcontentnode = errornode.SelectSingleNode("CodeContents/CodeContent");
                        if (errorcontentnode.Attributes.Count == 3)
                        {
                            if (texttype == 0)
                                errortext = errorcontentnode.Attributes[1].Value;
                            else
                                errortext = errorcontentnode.Attributes[2].Value;
                        }
                        else
                            errortext = errorcontentnode.Attributes[1].Value;
                    }
                }
                if (errortext == "")
                    errortext = errorcode;
                return errortext;
            }
            catch (Exception ex)
            {
                SendErrorMail(ex.Message, "TMEX.Common.GetErrorText错误");
                return ex.Message;
            }
        }
        #endregion

        #region 发送错误邮件
        public static void SendErrorMail(string msgbody, string title)
        {
            string smtpurl = System.Configuration.ConfigurationManager.AppSettings["smtpurl"];
            string smtpuser = System.Configuration.ConfigurationManager.AppSettings["smtpuser"];
            string smtppass = System.Configuration.ConfigurationManager.AppSettings["smtppass"];
            string erroremail = System.Configuration.ConfigurationManager.AppSettings["erroremail"];
            System.Net.Mail.SmtpClient client = new SmtpClient(smtpurl);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(smtpuser, smtppass);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            MailAddress MailFrom = new MailAddress("OTAErroReport@hubs2.com", "OTA接口错误");
            System.Net.Mail.MailMessage message = new MailMessage();
            message.From = MailFrom;
            message.To.Add(erroremail);
            message.Subject = title;
            message.Body += "<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd><html xmlns=http://www.w3.org/1999/xhtml>";
            message.Body += "<head>";
            message.Body += "<meta http-equiv=Content-Type content=text/html; charset=GB2312/>";
            message.Body += "<style type=text/css>";
            message.Body += "<!--";
            message.Body += "body,td,th {";
            message.Body += "font-size: 9pt;";
            message.Body += "font-family: Arial, Helvetica, sans-serif;";
            message.Body += "}";
            message.Body += "-->";
            message.Body += "</style></head>";
            message.Body += "<body>";
            message.Body += "<table width=98% border=0 cellpadding=5 cellspacing=5 bgcolor=#FFFFFF align=center>";
            message.Body += "  <tr>";
            message.Body += "    <td>";
            message.Body += msgbody;
            message.Body += "</td>";
            message.Body += "  </tr>";
            message.Body += "</table>";
            message.Body += "<p>&nbsp;</p>";
            message.Body += "<p>&nbsp;</p>";
            message.Body += "</body>";
            message.Body += "</html>";
            message.Priority = MailPriority.High;
            message.BodyEncoding = Encoding.GetEncoding("UTF-8");
            message.IsBodyHtml = true;
            client.Send(message);
        }
        #endregion
    }
}
